package com.fenghongzhang.day009;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button insert;
    private Button select;
    private Button update;
    private Button del;
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1,创建数据库
        MySqlHelp mySqlHelp = new MySqlHelp(this, "day.db", null, 1);
        //打开一个数据库
        db = mySqlHelp.getReadableDatabase();

        //关闭
        db.close();



        update = (Button) findViewById(R.id.update);
        del = (Button) findViewById(R.id.del);
        insert = (Button) findViewById(R.id.insert);
        select = (Button) findViewById(R.id.select);


        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1原生
//                String sql = "delete from student where id = ?";

//                db.execSQL(sql,new Object[]{"3"});

                //2
                db.delete("student","id=?",new String[]{"2"});
            }
        });


        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1 sql
//                String sql = "update student set age = ? where id = ?";
//                db.execSQL(sql,new Object[]{28,1});

                //2
                ContentValues contentValues = new ContentValues();
                contentValues.put("age",18);
                db.update("student",contentValues,"id=?",new String[]{"1"});
            }



        });


        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //对数据操作有两种方式.
                //1,原生sql
                String sql = "insert INTO student VALUES (null,?,?)";

                //开启
                db.beginTransaction();

                db.execSQL(sql,new Object[]{"小明",18});
                db.execSQL(sql,new Object[]{"小明",18});
                db.execSQL(sql,new Object[]{"小明",18});
                db.execSQL(sql,new Object[]{"小明",18});
                db.execSQL(sql,new Object[]{"小明",18});
                db.execSQL(sql,new Object[]{"小明",18});
                db.execSQL(sql,new Object[]{"小明",18});
                db.execSQL(sql,new Object[]{"小明",18});
                db.execSQL(sql,new Object[]{"小明",18});

                //提交
                db.setTransactionSuccessful();

                //关闭
                db.endTransaction();

                //2,提供的api
//                ContentValues contentValues = new ContentValues();
//                //字段,值
//                contentValues.put("name","大明");
//                contentValues.put("age",20);
//                /**
//                 * 表
//                 * null
//                 * 字段->值
//                 */
//                db.insert("student",null,contentValues);
            }
        });


        //查询
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1,原生
//                String sql = "select * from student"; //表
//                //游标
//                Cursor cursor = db.rawQuery(sql, null);
//                if(cursor != null){
//                    while(cursor.moveToNext()){ //把游标执行下一行
//                        String name = cursor.getString(cursor.getColumnIndex("name"));//1
//                        Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
//                    }
//                }


                //2 api
                Cursor student = db.query("student", null, null, null, null, null, null);
                if(student != null){
                    while (student.moveToNext()){
                        String id = student.getString(student.getColumnIndex("id"));
                        String name = student.getString(student.getColumnIndex("name"));
                        String age = student.getString(student.getColumnIndex("age"));

                        Toast.makeText(MainActivity.this, id+name+age, Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}
